﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MIMSDomain.Entities;
using MIMSDomain.Abstract;
using System.Linq.Expressions;

namespace MIMSDomain.Concrete
{
    public class EFOccupationRepository : IRepository<Occupation>, IDisposable
    {

        EFDbContext dbContext = new EFDbContext();
        
        public virtual IQueryable<Occupation> All
        {
            get
            {
                return dbContext.Occupations;
            }
            
        }
        public virtual bool Save(Occupation _T)
        {
            try
            {
                if (_T.Id == 0)
                {
                    dbContext.Occupations.Add(_T);
                    dbContext.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception Ex)
            {
                //Log Exception
                throw new Exception(Ex.Message);
            }
            finally
            {
                //clean ups
            }
        }
        public virtual Occupation GetById(int Id)
        {
            try
            {
                return dbContext.Occupations.Find(Id);
            }
            catch (Exception Ex)
            {
                //Log Exception
                throw new Exception(Ex.Message);
            }
            finally
            {

            }
        }
        public virtual bool Delete(Occupation _T)
        {
            try
            {
                dbContext.Occupations.Remove(_T);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception Ex)
            {
                //Log Exception
                throw new Exception(Ex.Message);
            }
            finally
            {

            }
        }

        public IQueryable<Occupation> Search(Expression<Func<Occupation, bool>> predicate)
        {
            return dbContext.Occupations.Where(predicate);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                dbContext.Dispose();
            }
        }
    }
}
